﻿Ext.define('app.prm.detail.East', {
    extend: 'Ext.panel.Panel',
    region: 'east',
    split: false,
    width: 300,
    border: true,
    scrollable: 'vertical',
    projectId: 0,
    //bodyStyle: 'padding:7px 0 0 0',
    //layout: 'accordion',
    //layoutConfig: {
    //    activeOnTop: true,  //设置打开的字面板置顶  
    //    collapseFirst: false,
    //    fill: true,         //字面板充满父面板的剩余空间  
    //    hideCollapseTool: false,   //显示“展开收缩”按钮  
    //    titleCollapse: true,      //允许通过点击子面板的标题来展开或者收缩面板  
    //    animate: true             //使用动画效果  
    //},
    initComponent: function () {
        var me = this;
        me.items = [me.getProjectStatus(), me.getProjectCost(), me.getProjectTeam(), me.getProjectQuan()];
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            me.initStatus();
            me.initTeam();
            me.initQuan();
        }
    },
    getProjectStatus: function () {
        var me = this;
        if (!this.prmStatus) {
            this.prmStatus = new Ext.form.Panel({
                defaultType: 'displayfield',
                bodyStyle: 'padding:10px',
                border: true,
                title: localize('Status'),
                defaults: {
                    xtype: 'displayfield',
                    labelAlign: 'right',
                    labelWidth: 80
                },
                //collapsed: false,
                //collapsible: false,
                tools: [
                {
                    type: 'gear',
                    bind: {
                        hidden: '{!hasEditProgress}'
                    },
                    tooltip: localize('Edit'),
                    callback: function (panel, tool, event) {
                        var win = Ext.create("app.prm.detail.EditProgress", { projectId: me.projectId });
                        win.on("savesuccess", function (formValues) {
                            me.initStatus();
                            win.close();
                        });
                        win.show();
                    }
                }],
                items: [
                    { fieldLabel: localize('Status'), name: 'status' },
                    //{ fieldLabel: '进度状态'},
                    { fieldLabel: localize('ProgressStatus'), name: 'progress' },
                    { fieldLabel: localize('ProgressDescription'), name: 'note' }
                ]
            });
        }
        return this.prmStatus;
    },
    getProjectCost: function () {
        var me = this;
        if (!this.prmCost) {
            this.prmCost = new Ext.form.Panel({
                defaultType: 'displayfield',
                bodyStyle: 'padding:10px',
                border: true,
                title: localize('ProjectCost'),
                // collapsible: true,
                //collapsed: false,
                tools: [
                {
                    type: 'gear',
                    bind: {
                        hidden: '{!hasEditCost}'
                    },
                    tooltip: localize('Edit'),
                    callback: 'onEditCose'
                }],
                items: [
                    { fieldLabel: '项目成本', name: 'actualCost', bind: { value: '{actualCost}/{targetCost}' } },
                    { fieldLabel: '实际人力成本', name: 'actualWorkCost', bind: { value: '{actualWorkCost}/{targetWorkCost}' } },
                    { fieldLabel: '实际完成量', name: 'actualOutPut', bind: { value: '{actualOutPut}/{targetOutPut}' } },
                    { fieldLabel: '实际原料成本', name: 'actualMaterialCost', bind: { value: '{actualMaterialCost}/{targetMaterialCost}' } }
                ]
            });
        }
        return this.prmCost;
    },
    initStatus: function () {
        var me = this;
        App.AjaxGet('/api/services/app/project/GetLastProgressOutPut?projectId=' + me.projectId, function (data) {
            console.log("---------------------------------------");
            console.log(data);
            console.log("---------------------------------------");
            me.prmStatus.getForm().setValues(data.result);
        });
    },
    getProjectTeam: function () {
        var me = this;
        if (!this.prmTeam) {
            this.prmTeam = new Ext.panel.Panel({
                bodyStyle: 'padding:10px',
                border: false,
                title: '项目成员',
                tools: [
                {
                    type: 'gear',
                    tooltip: '编辑',
                    bind: {
                        hidden: '{!hasEditTeam}'
                    },
                    callback: function (panel, tool, event) {
                        var win = Ext.create("app.prm.detail.SelectTeam", { title: '编辑成员', projectId: me.projectId });
                        win.on("savesuccess", function () {
                            me.initTeam();
                            win.close();
                        });
                        win.show();
                    }
                }],
                items: []
            });
        }
        return this.prmTeam;
    },
    getProjectQuan: function () {
        var me = this;
        if (!this.prmQuan) {
            this.prmQuan = new Ext.panel.Panel({
                bodyStyle: 'padding:10px',
                border: false,
                title: '项目区域',
                tools: [
                {
                    type: 'gear',
                    tooltip: '编辑',
                    bind: {
                        hidden: '{!hasEditTeam}'
                    },
                    callback: function (panel, tool, event) {
                        var win = Ext.create("app.prm.detail.EditQuan", { title: '编辑区域', projectId: me.projectId });
                        win.on("savesuccess", function () {
                            win.close();
                            me.initQuan();
                        });
                        win.show();
                    }
                }],
                items: []
            });
        }
        return this.prmQuan;
    },
    initQuan: function () {
        var me = this;
        App.AjaxGet("/api/services/app/quan/GetProjectQuanList?projectId=" + me.projectId, function (data) {
            me.getProjectQuan().removeAll(true);
            console.log(data);

            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];
                var panel = new Ext.panel.Panel({
                    border: false,
                    style: 'margin:0 0 10px 0',
                    items: [
                        { border: false, html: '<b>' + record + '</b>' },
                    ]
                });
                me.getProjectQuan().add(panel);
            }
        });
    },
    initTeam: function () {
        var me = this; 
        App.AjaxGet("/api/services/app/project/GetProjectTeam?projectId=" + me.projectId, function (data) {
            me.getProjectTeam().removeAll(true);
            console.log(data);
            var dtNow = new Date();
            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];
                var panel = new Ext.panel.Panel({
                    border: false,
                    style: 'margin:0 0 10px 0',
                    items: [
                        { border: false, html: '<b>' + record.roleName + '</b>' },
                    ]
                });

                for (var j = 0; j < record.teams.length; j++) {

                    var team = record.teams[j];
                    var dteTeam = new Date(team.dte);
                    var isLeave = dteTeam <= dtNow;
                    panel.add(new Ext.panel.Panel({
                        border: false,
                        layout: 'column',
                        items: [
                            {
                                border: false,
                                html: (record.roleName.indexOf('访客') > -1 || isLeave) ? team.userName + " (" + team.dts + "~" + team.dte + ")" : team.userName
                            },
                            {
                                border: false,
                                projectRoleId: team.itemId,
                                bind: { hidden: '{!hasDeleteTeam}' },
                                //hidden: record.roleName.indexOf('访客') > -1||isLeave||,
                                html: (record.roleName.indexOf('访客') > -1 || isLeave) ?'':'&nbsp;&nbsp;<a style="text-decoration: underline;cursor:pointer">退出</a>',
                                listeners: {
                                    afterrender: function (component) {
                                        component.getEl().on('click', function () {
                                            Ext.Msg.confirm('系统提示', '是否确认退出项目?', function (btn) {
                                                if (btn == 'yes') {
                                                    me.leavePrmRole(component, me.projectId);
                                                }
                                            });
                                        });
                                    }
                                }
                            },
                            {
                                border: false,
                                projectRoleId: team.itemId,
                                bind: { hidden: '{!hasDeleteTeam}' },
                                html: isLeave ? "" : '&nbsp;<a style="text-decoration: underline;cursor:pointer;">删除</a>',
                                listeners: {
                                    afterrender: function (component) {
                                        component.getEl().on('click', function () {
                                            Ext.Msg.confirm('系统提示', '是否确认删除?', function (btn) {
                                                if (btn == 'yes') {
                                                    me.delPrmRole(component, me.projectId);
                                                }
                                            });
                                        });
                                    }
                                }
                            }
                        ]
                    }));
                }
                me.getProjectTeam().add(panel);
            }
        });
    },
    delPrmRole: function (component, projectId) {
        var me = this;
        App.AjaxDelete('/api/services/app/project/RemoveProjectTeam?projectRoleId=' + component.projectRoleId + "&projectId=" + projectId, function () {
            me.initTeam();
        });
    },
    leavePrmRole: function (component, projectId) {
        var me = this;
        App.AjaxPost('/api/services/app/project/LeaveProjectTeam?projectRoleId='
            + component.projectRoleId + "&projectId=" + projectId,null,
            function () {
            me.initTeam();
        });
    }
})